Imports System.Collections.Generic

Public Class ACControlEntidad
#Region " Variables "
   Private m_codigo As String
   Private m_descripcion As String
   Private m_documento As String

   Private m_direcciones As DataTable
   Private m_direcdisplamember As String = "DIRE_Desc"
   Private m_direcvaluemember As String = "DIRE_Item"

   Private m_tipoentidad As DataTable
   Private m_tipodisplamember As String = "TIPE_Codigo"
   Private m_tipovaluemember As String = "TIPE_Desc"

   Private m_entidad As ACEEntidad
   Private m_listdireccciones As List(Of ACEDirecEntc)
   Private m_listtipoentidad As List(Of ACETipoEntidad)
   Private m_class As Object

   Public Event ACCodigo_Click(ByVal sender As Object, ByVal e As System.EventArgs)
   Public Event ACDocumento_Click(ByVal sender As Object, ByVal e As System.EventArgs)
   Public Event ACRazonSocial_Click(ByVal sender As Object, ByVal e As System.EventArgs)
   Public Event ACDirecciones_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
   Public Event ACTipoEntidad_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
   Public Event ACENTC_Codigo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
   Public Event ACENTC_DocIden_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
   Public Event ACENTC_RazonSocial_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

   Public Event ACTextComplete(ByVal sender As System.Object, ByVal e As System.EventArgs)

   Enum TipoCarga
      All
      CodDesDoc
      ValueTwo
   End Enum

   Private m_longitudcodigo As Integer = 12
   Private m_activarautoayudaenter As Boolean = False

#End Region

#Region " Propiedades "

   Public Property ACENTC_DocIden() As String
      Get
         Return m_documento
      End Get
      Set(ByVal value As String)
         m_documento = value
         m_entidad.ENTC_DocIden = value
      End Set
   End Property

   Public Property ACENTC_RazonSocial() As String
      Get
         Return m_descripcion
      End Get
      Set(ByVal value As String)
         m_descripcion = value
         m_entidad.ENTC_RazonSoc = value
      End Set
   End Property

   Public Property ACENTC_Codigo() As String
      Get
         Return m_codigo
      End Get
      Set(ByVal value As String)
         m_codigo = value
         m_entidad.ENTC_Codigo = value
      End Set
   End Property

   Public Property ACDirecEntc() As DataTable
      Get
         Return m_direcciones
      End Get
      Set(ByVal value As DataTable)
         m_direcciones = value
         If IsNothing(m_direcciones) Then
            cmbDireccion.Enabled = False
         Else
            cmbDireccion.Enabled = True
            m_listdireccciones = New List(Of ACEDirecEntc)()
            For Each Item As DataRowView In m_direcciones.Rows
               m_listdireccciones.Add(New ACEDirecEntc(m_codigo, Item(m_direcvaluemember), Item(m_direcdisplamember)))
            Next
         End If
      End Set
   End Property

   Public Property ACDirecValueMember() As String
      Get
         Return m_direcvaluemember
      End Get
      Set(ByVal value As String)
         m_direcvaluemember = value
      End Set
   End Property

   Public Property ACDirecDisplayMember() As String
      Get
         Return m_direcdisplamember
      End Get
      Set(ByVal value As String)
         m_direcdisplamember = value
      End Set
   End Property

   Public Property ACTipoEntidad() As DataTable
      Get
         Return m_tipoentidad
      End Get
      Set(ByVal value As DataTable)
         m_tipoentidad = value
         If IsNothing(m_tipoentidad) Then
            pnlTipoEntidad.Visible = False
         Else
            m_listtipoentidad = New List(Of ACETipoEntidad)()
            For Each Item As DataRowView In m_tipoentidad.Rows
               m_listtipoentidad.Add(New ACETipoEntidad(Item(m_tipovaluemember), Item(m_tipodisplamember)))
            Next
            pnlTipoEntidad.Visible = True
         End If
      End Set
   End Property

   Public Property ACTipoValueMember() As String
      Get
         Return m_tipovaluemember
      End Get
      Set(ByVal value As String)
         m_tipovaluemember = value
      End Set
   End Property

   Public Property ACTipoDisplayMember() As String
      Get
         Return m_tipodisplamember
      End Get
      Set(ByVal value As String)
         m_tipodisplamember = value
      End Set
   End Property

   Public Property ACEEntidad() As ACEEntidad
      Get
         Return m_entidad
      End Get
      Set(ByVal value As ACEEntidad)
         m_entidad = value
      End Set
   End Property

   Public Property ACListDirecciones() As List(Of ACEDirecEntc)
      Get
         Return m_listdireccciones
      End Get
      Set(ByVal value As List(Of ACEDirecEntc))
         m_listdireccciones = value
      End Set
   End Property

   Public Property ACListTipoEntidad() As List(Of ACETipoEntidad)
      Get
         Return m_listtipoentidad
      End Get
      Set(ByVal value As List(Of ACETipoEntidad))
         m_listtipoentidad = value
      End Set
   End Property

   Public Property ACClase() As Object
      Get
         Return m_class
      End Get
      Set(ByVal value As Object)
         m_class = value
      End Set
   End Property

   Public Property ACLongitudCodigo() As Integer
      Get
         Return m_longitudcodigo
      End Get
      Set(ByVal value As Integer)
         m_longitudcodigo = value
      End Set
   End Property

   Public Property ActivarAutoAyudaEnter() As Boolean
      Get
         Return m_activarautoayudaenter
      End Get
      Set(ByVal value As Boolean)
         m_activarautoayudaenter = value
      End Set
   End Property


#Region " Controles "
#Region " GroupBox "
   Public Property ACTitulo() As String
      Get
         Return grpEntidad.Text
      End Get
      Set(ByVal value As String)
         grpEntidad.Text = value
      End Set
   End Property

   Public Property ACBackColor() As Color
      Get
         Return grpEntidad.BackColor
      End Get
      Set(ByVal value As Color)
         grpEntidad.BackColor = value
      End Set
   End Property

   Public Property ACForeColor() As Color
      Get
         Return grpEntidad.ForeColor
      End Get
      Set(ByVal value As Color)
         grpEntidad.ForeColor = value
      End Set
   End Property
#End Region

   Public Property ACVerTipoEntidad() As Boolean
      Get
         Return pnlTipoEntidad.Visible
      End Get
      Set(ByVal value As Boolean)
         pnlTipoEntidad.Visible = value
      End Set
   End Property

   Public Property ACTextTipoEntidad() As System.Windows.Forms.ComboBox
      Get
         Return cmbTipoEntidad
      End Get
      Set(ByVal value As System.Windows.Forms.ComboBox)
         cmbTipoEntidad = value
      End Set
   End Property

   Public Property ACTextCodigo() As ACTextBoxAyuda
      Get
         Return nstxaCodigo
      End Get
      Set(ByVal value As ACTextBoxAyuda)
         nstxaCodigo = value
      End Set
   End Property

   Public Property ACTextDocumento() As ACTextBoxAyuda
      Get
         Return nstxaNumero
      End Get
      Set(ByVal value As ACTextBoxAyuda)
         nstxaNumero = value
      End Set
   End Property

   Public Property ACTextRazonSocial() As ACTextBoxAyuda
      Get
         Return nstxaRazonSocial
      End Get
      Set(ByVal value As ACTextBoxAyuda)
         nstxaRazonSocial = value
      End Set
   End Property

#Region " ComboBox "
   Public Property ACCmbDirecciones() As System.Windows.Forms.ComboBox
      Get
         Return cmbDireccion
      End Get
      Set(ByVal value As System.Windows.Forms.ComboBox)
         cmbDireccion = value
      End Set
   End Property

   Public Property ACDirecSelectedValue() As Object
      Get
         Return cmbDireccion.SelectedValue
      End Get
      Set(ByVal value As Object)
         cmbDireccion.SelectedValue = value
      End Set
   End Property

   Public Property ACDirecSelectedItem() As Object
      Get
         Return cmbDireccion.SelectedItem
      End Get
      Set(ByVal value As Object)
         cmbDireccion.SelectedItem = value
      End Set
   End Property

   Public Property ACTipoSelectedValue() As Object
      Get
         Return cmbTipoEntidad.SelectedValue
      End Get
      Set(ByVal value As Object)
         cmbTipoEntidad.SelectedValue = value
      End Set
   End Property

   Public Property ACTipoSelectedItem() As Object
      Get
         Return cmbTipoEntidad.SelectedItem
      End Get
      Set(ByVal value As Object)
         cmbTipoEntidad.SelectedItem = value
      End Set
   End Property
#End Region
#End Region

#End Region

#Region " Constructores "
   Public Sub New()

      ' This call is required by the Windows Form Designer.
      InitializeComponent()
      m_entidad = New ACEEntidad()
      m_listdireccciones = New List(Of ACEDirecEntc)
      m_listtipoentidad = New List(Of ACETipoEntidad)

      Me.Size() = New System.Drawing.Size(500, 70)
      ' Add any initialization after the InitializeComponent() call.

   End Sub

   Public Sub Inicializar(ByVal x_entc_codigo As String, ByVal x_entc_razonsoc As String, ByVal x_entc_dociden As String, ByVal x_direcciones As DataTable, ByVal x_tipoentidad As DataTable)
      Try
         m_entidad = New ACEEntidad()
         ACENTC_Codigo = x_entc_codigo
         ACENTC_RazonSocial = x_entc_razonsoc
         ACENTC_DocIden = x_entc_dociden
         ACDirecEntc = x_direcciones
         setControles(TipoCarga.All)
      Catch ex As Exception
         Throw ex
      End Try
   End Sub

   Public Sub Inicializar(ByVal x_entc_codigo As String, ByVal x_entc_razonsoc As String, ByVal x_entc_dociden As String, ByVal x_direcciones As DataTable)
      Try
         m_entidad = New ACEEntidad()
         ACENTC_Codigo = x_entc_codigo
         ACENTC_RazonSocial = x_entc_razonsoc
         ACENTC_DocIden = x_entc_dociden
         ACDirecEntc = x_direcciones
         setControles(TipoCarga.All)
      Catch ex As Exception
         Throw ex
      End Try
   End Sub

   Public Sub Inicializar(ByVal x_entc_codigo As String, ByVal x_entc_razonsoc As String, ByVal x_entc_dociden As String)
      Try
         ACENTC_Codigo = x_entc_codigo
         ACENTC_RazonSocial = x_entc_razonsoc
         ACENTC_DocIden = x_entc_dociden
         cmbDireccion.Enabled = False
         setControles(TipoCarga.CodDesDoc)
      Catch ex As Exception
         Throw ex
      End Try
   End Sub
#End Region

#Region " Metodos "
   Private Sub setControles(ByVal m_tipo As TipoCarga)
      Try
         Select Case m_tipo
            Case TipoCarga.All
               nstxaCodigo.Text = m_codigo
               nstxaNumero.Text = m_documento
               nstxaRazonSocial.Text = m_descripcion
               ACFramework.ACUtilitarios.ACCargaCombo(cmbDireccion, m_listdireccciones, m_direcdisplamember, m_direcvaluemember)
            Case TipoCarga.CodDesDoc
               nstxaCodigo.Text = m_codigo
               nstxaNumero.Text = m_documento
               nstxaRazonSocial.Text = m_descripcion
            Case Else
               nstxaCodigo.Text = m_codigo
               nstxaNumero.Text = m_documento
               nstxaRazonSocial.Text = m_descripcion
         End Select
      Catch ex As Exception
         Throw ex
      End Try
   End Sub

   Public Sub ACLimpiar()
      Me.nstxaCodigo.Text = ""
      Me.nstxaNumero.Text = ""
      Me.nstxaRazonSocial.Text = ""
      If Not IsNothing(m_listdireccciones) Then
         m_listdireccciones = New List(Of ACEDirecEntc)()
         Me.cmbDireccion.DataSource = m_listdireccciones
      End If
   End Sub
#End Region

#Region " Metodos de Controles"

#End Region

#Region " Eventos Botones "
   Private Sub txtCodigo_ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nstxaCodigo.ACAyudaClick
      If m_longitudcodigo = Me.nstxaCodigo.Text.Trim().Length Then
         RaiseEvent ACCodigo_Click(sender, e)
      End If
   End Sub

   Private Sub txtDocumento_ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nstxaNumero.ACAyudaClick
      RaiseEvent ACDocumento_Click(sender, e)
   End Sub

   Private Sub txtRazonSocial_ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nstxaRazonSocial.ACAyudaClick
      RaiseEvent ACRazonSocial_Click(sender, e)
   End Sub

   Private Sub cmbDireccion_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDireccion.SelectedIndexChanged
      RaiseEvent ACDirecciones_SelectedIndexChanged(sender, e)
   End Sub

   Private Sub cmbTipoEntidad_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTipoEntidad.SelectedIndexChanged
      RaiseEvent ACTipoEntidad_SelectedIndexChanged(sender, e)
   End Sub

   Private Sub nstxaCodigo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nstxaCodigo.TextChanged
      m_codigo = nstxaCodigo.Text
      If (m_codigo.Trim().Length = m_longitudcodigo) Then
         RaiseEvent ACTextComplete(sender, e)
      End If
      RaiseEvent ACENTC_Codigo_TextChanged(sender, e)
   End Sub

   Private Sub nstxaNumero_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nstxaNumero.TextChanged
      m_documento = nstxaNumero.Text
      RaiseEvent ACENTC_DocIden_TextChanged(sender, e)
   End Sub

   Private Sub nstxaRazonSocial_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nstxaRazonSocial.TextChanged
      m_descripcion = nstxaRazonSocial.Text
      RaiseEvent ACENTC_RazonSocial_TextChanged(sender, e)
   End Sub



#End Region
End Class
